package dao;

import org.apache.ibatis.annotations.*;
import pojo.Jxb;

import java.util.List;

@Mapper
/**
 * 急性病表数据访问接口
 */
public interface JxbMapper {
    // 根据id查询疾病
    @Select("SELECT " +
            "id, " +
            "name, " +
            "category, " +
            "high_season, " +
            "description, " +
            "cause, " +
            "prevention, " +
            "treatment, " +
            "view_count, " +
            "create_time, " +
            "trend, " +
            "severity, " +
            "cover_img " +
            "FROM jxb WHERE id = #{id}")
    Jxb findJxbById(Integer id);

    // 查询所有疾病
    @Select("SELECT " +
            "id, " +
            "name, " +
            "category, " +
            "high_season, " +
            "description, " +
            "cause, " +
            "prevention, " +
            "treatment, " +
            "view_count, " +
            "create_time, " +
            "trend, " +
            "severity, " +
            "cover_img " +
            "FROM jxb")
    List<Jxb> findAllJxb();

    // 按分类查询疾病
    @Select("SELECT " +
            "id, " +
            "name, " +
            "category, " +
            "high_season, " +
            "description, " +
            "cause, " +
            "prevention, " +
            "treatment, " +
            "view_count, " +
            "create_time, " +
            "trend, " +
            "severity, " +
            "cover_img " +
            "FROM jxb WHERE category = #{category}")
    List<Jxb> findJxbByCategory(String category);

    // 按季节查询高发疾病
    @Select("SELECT " +
            "id, " +
            "name, " +
            "category, " +
            "high_season, " +
            "description, " +
            "cause, " +
            "prevention, " +
            "treatment, " +
            "view_count, " +
            "create_time, " +
            "trend, " +
            "severity, " +
            "cover_img " +
            "FROM jxb WHERE high_season LIKE CONCAT('%', #{season}, '%')")
    List<Jxb> findJxbBySeason(String season);

    // 搜索疾病
    @Select("SELECT " +
            "id, " +
            "name, " +
            "category, " +
            "high_season, " +
            "description, " +
            "cause, " +
            "prevention, " +
            "treatment, " +
            "view_count, " +
            "create_time, " +
            "trend, " +
            "severity, " +
            "cover_img " +
            "FROM jxb WHERE " +
            "name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "description LIKE CONCAT('%', #{keyword}, '%') OR " +
            "cause LIKE CONCAT('%', #{keyword}, '%')")
    List<Jxb> searchJxb(String keyword);

    // 新增疾病
    @Insert("INSERT INTO jxb (" +
            "name, category, high_season, description, cause, " +
            "prevention, treatment, view_count, create_time, trend, " +
            "severity, cover_img) " +
            "VALUES (" +
            "#{name}, #{category}, #{highSeason}, #{description}, #{cause}, " +
            "#{prevention}, #{treatment}, #{viewCount}, NOW(), #{trend}, " +
            "#{severity}, #{coverImg})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addJxb(Jxb jxb);

    // 更新疾病
    @Update("UPDATE jxb SET " +
            "name = #{name}, " +
            "category = #{category}, " +
            "high_season = #{highSeason}, " +
            "description = #{description}, " +
            "cause = #{cause}, " +
            "prevention = #{prevention}, " +
            "treatment = #{treatment}, " +
            "view_count = #{viewCount}, " +
            "trend = #{trend}, " +
            "severity = #{severity}, " +
            "cover_img = #{coverImg} " +
            "WHERE id = #{id}")
    int updateJxb(Jxb jxb);

    // 删除疾病
    @Delete("DELETE FROM jxb WHERE id = #{id}")
    int deleteJxb(Integer id);

    // 更新访问量
    @Update("UPDATE jxb SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(Integer id);
}